package com.smartcruise.main.mainFragment.tools.map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.amap.api.maps.model.LatLng;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewListener;
import com.amap.api.navi.enums.NaviType;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapModelCross;
import com.amap.api.navi.model.AMapNaviCameraInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AMapServiceAreaInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.autonavi.tbt.TrafficFacilityInfo;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.NormalListDialog;
import com.smartcruise.R;
import com.smartcruise.base.BaseTitleActivity;
import com.smartcruise.base.TitleBuilder;
import com.smartcruise.util.IntentUtils;
import com.smartcruise.util.Utils;

import java.util.Collections;

import butterknife.BindView;

/**
 * Created by FengChaoQun
 * on 2018/3/24 0024
 * 导航页面
 */

public class NavigateActivity extends BaseTitleActivity implements AMapNaviViewListener, AMapNaviListener {
    @BindView(R.id.navigationView)
    AMapNaviView mAMapNaviView;

    private LatLng endPoint;
    private AMapNavi mAMapNavi;
    private String dName;//终点名称

    public static void start(Context context, LatLng latLng, String dName) {
        Intent intent = new Intent(context, NavigateActivity.class);
        intent.putExtra(IntentUtils.DATA, latLng);
        intent.putExtra("name", dName);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setEnableRightSlide(false);
        mAMapNaviView.onCreate(savedInstanceState);
        endPoint = getIntent().getParcelableExtra(IntentUtils.DATA);
        dName = getIntent().getStringExtra("name");
        if (endPoint == null) {
            showToast("没有指定终点");
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAMapNaviView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mAMapNaviView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAMapNaviView.onDestroy();

        mAMapNavi.stopNavi();
        mAMapNavi.destroy();
    }

    @Override
    protected void init() {

        mAMapNaviView.setAMapNaviViewListener(this);

        mAMapNavi = AMapNavi.getInstance(getApplicationContext());
        mAMapNavi.setAMapNaviListener(this);
        mAMapNavi.setUseInnerVoice(true);
    }


    @Override
    protected int getContentLayout() {
        return R.layout.activity_content_navigate;
    }

    @Override
    protected TitleBuilder getTitleBuilder() {
        TitleBuilder titleBuilder = TitleBuilder.create(true, "导航");
        titleBuilder.setShowRightImage(true);
        titleBuilder.setRightImageSrc(R.drawable.ic_action_scan);
        return titleBuilder;
    }

    @Override
    public void onRightImageClicked() {
        String[] strings = new String[]{"步行导航", "骑行导航", "驾车导航", "第三方导航"};
        final NormalListDialog normalListDialog = new NormalListDialog(getActivity(), strings);
        normalListDialog.title("选择导航模式");
        normalListDialog.layoutAnimation(null);
        normalListDialog.setOnOperItemClickL(new OnOperItemClickL() {
            @Override
            public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        mAMapNavi.calculateWalkRoute(new NaviLatLng(endPoint.latitude, endPoint.longitude));
                        break;
                    case 1:
                        mAMapNavi.calculateRideRoute(new NaviLatLng(endPoint.latitude, endPoint.longitude));
                        break;
                    case 2:
                        searchDriverRoute();
                        break;
                    case 3:
                        showMapAppList();
                        break;
                }
                normalListDialog.dismiss();
            }
        });
        normalListDialog.show();
    }

    private void showMapAppList() {
        final String[] mapAPPs = Utils.getMapAppArrays(getActivity());
        if (mapAPPs == null || mapAPPs.length == 0) {
            showToast("没有在你的手机上找到地图APP");
            return;
        }
        final NormalListDialog normalListDialog = new NormalListDialog(getActivity(), mapAPPs);
        normalListDialog.title("选择地图");
        normalListDialog.layoutAnimation(null);
        normalListDialog.setOnOperItemClickL(new OnOperItemClickL() {
            @Override
            public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mapAPPs[position].equals("百度地图")) {
                    Utils.goToBaiduNaviActivity(getApplicationContext(), dName, endPoint.latitude, endPoint.longitude);
                } else if (mapAPPs[position].equals("高德地图")) {
                    Utils.goToGaodeNaviActivity(getApplicationContext(), endPoint.latitude, endPoint.longitude, dName);
                }
                normalListDialog.dismiss();
            }
        });
        normalListDialog.show();
    }

    private void searchDriverRoute() {
        /**
         * 方法:
         *   int strategy=mAMapNavi.strategyConvert(congestion, avoidhightspeed, cost, hightspeed, multipleroute);
         * 参数:
         * @congestion 躲避拥堵
         * @avoidhightspeed 不走高速
         * @cost 避免收费
         * @hightspeed 高速优先
         * @multipleroute 多路径
         *
         * 说明:
         *      以上参数都是boolean类型，其中multipleroute参数表示是否多条路线，如果为true则此策略会算出多条路线。
         * 注意:
         *      不走高速与高速优先不能同时为true
         *      高速优先与避免收费不能同时为true
         */
        int strategy = 0;
        try {
            strategy = mAMapNavi.strategyConvert(true, false, false, false, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mAMapNavi.calculateDriveRoute(Collections.singletonList(new NaviLatLng(endPoint.latitude, endPoint.longitude))
                , null, strategy);
    }

    @Override
    public void onNaviSetting() {

    }

    @Override
    public void onNaviCancel() {

    }

    @Override
    public boolean onNaviBackClick() {
        return false;
    }

    @Override
    public void onNaviMapMode(int i) {

    }

    @Override
    public void onNaviTurnClick() {

    }

    @Override
    public void onNextRoadClick() {

    }

    @Override
    public void onScanViewButtonClick() {

    }

    @Override
    public void onLockMap(boolean b) {

    }

    @Override
    public void onNaviViewLoaded() {

    }

    @Override
    public void onInitNaviFailure() {

    }

    @Override
    public void onInitNaviSuccess() {
        //步行路径计算
        mAMapNavi.calculateWalkRoute(new NaviLatLng(endPoint.latitude, endPoint.longitude));
        Log.d("NavigateActivity", "开始计算路径");
    }

    @Override
    public void onStartNavi(int i) {

    }

    @Override
    public void onTrafficStatusUpdate() {

    }

    @Override
    public void onLocationChange(AMapNaviLocation aMapNaviLocation) {

    }

    /**
     * @param i
     * @param s
     * @deprecated
     */
    @Override
    public void onGetNavigationText(int i, String s) {

    }

    @Override
    public void onGetNavigationText(String s) {

    }

    @Override
    public void onEndEmulatorNavi() {

    }

    @Override
    public void onArriveDestination() {

    }

    @Override
    public void onCalculateRouteFailure(int i) {
        switch (i) {
            case 2:
                showToast("网络异常,请检查网络是否通畅，稍候再试。");
                break;
            default:
                showToast("计算路径失败:错误码" + i);
        }
    }

    @Override
    public void onReCalculateRouteForYaw() {

    }

    @Override
    public void onReCalculateRouteForTrafficJam() {

    }

    @Override
    public void onArrivedWayPoint(int i) {

    }

    @Override
    public void onGpsOpenStatus(boolean b) {

    }

    @Override
    public void onNaviInfoUpdate(NaviInfo naviInfo) {

    }

    /**
     * @param aMapNaviInfo
     * @deprecated
     */
    @Override
    public void onNaviInfoUpdated(AMapNaviInfo aMapNaviInfo) {

    }

    @Override
    public void updateCameraInfo(AMapNaviCameraInfo[] aMapNaviCameraInfos) {

    }

//    @Override
//    public void updateIntervalCameraInfo(AMapNaviCameraInfo aMapNaviCameraInfo, AMapNaviCameraInfo aMapNaviCameraInfo1, int i) {
//
//    }

    @Override
    public void onServiceAreaUpdate(AMapServiceAreaInfo[] aMapServiceAreaInfos) {

    }

    @Override
    public void showCross(AMapNaviCross aMapNaviCross) {

    }

    @Override
    public void hideCross() {

    }

    @Override
    public void showModeCross(AMapModelCross aMapModelCross) {

    }

    @Override
    public void hideModeCross() {

    }

    /**
     * @param aMapLaneInfos
     * @param bytes
     * @param bytes1
     * @deprecated
     */
    @Override
    public void showLaneInfo(AMapLaneInfo[] aMapLaneInfos, byte[] bytes, byte[] bytes1) {

    }

//    @Override
//    public void showLaneInfo(AMapLaneInfo aMapLaneInfo) {
//
//    }

    @Override
    public void hideLaneInfo() {

    }

    @Override
    public void onCalculateRouteSuccess(int[] ints) {
        mAMapNavi.stopNavi();
        mAMapNavi.startNavi(NaviType.GPS);
    }

    @Override
    public void notifyParallelRoad(int i) {

    }

    /**
     * @param aMapNaviTrafficFacilityInfo
     * @deprecated
     */
    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {

    }

    /**
     * @param trafficFacilityInfo
     * @deprecated
     */
    @Override
    public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {

    }

    @Override
    public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {

    }

    @Override
    public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {

    }

    @Override
    public void onPlayRing(int i) {

    }
}
